package com.zl.tby.controller;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyCameraInfoQuery;
import com.zl.tby.dto.TbyCategoryDictQuery;
import com.zl.tby.model.TbyCategoryDict;
import com.zl.tby.service.ITbyCategoryDictService;
import com.zl.tby.vo.TbyCategoryDictBean;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 多级分类字典
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Valid
@Controller
@Mapping("/category_dict")
public class TbyCategoryDictController {

    @Inject
    private ITbyCategoryDictService tbyCategoryDictService;

    /**
     * 保存或新增面积
     * @param entity: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/save", method = MethodType.POST)
    public R<TbyCategoryDict> saveOrUpdate(@Validated TbyCategoryDict entity) {
        try {
            if (entity.getId() == null) {
                return tbyCategoryDictService.add(entity);
            }
            return tbyCategoryDictService.update(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyCategoryDict> get(@NotNull Long id) {
        TbyCategoryDict entity = this.tbyCategoryDictService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return tbyCategoryDictService.delete(id);
    }

    /**
     * 树形表格数据
     * @return 分页结果
     */
    @Mapping(value = "/tree_table_list", method = MethodType.GET)
    public R<TbyPageResult<TbyCategoryDictBean>> treeTableList(@Body TbyCategoryDictQuery query) {
        return R.ok(this.tbyCategoryDictService.treeTableList(query));
    }

    /**
     * 下拉选数据
     * @return 结果
     */
    @Mapping(value = "/options", method = MethodType.GET)
    public R<List<OptionType>> options(@Body TbyCategoryDictQuery query) {
        return R.ok(this.tbyCategoryDictService.options(query));
    }

    /**
     * 验证编码是否存在
     * @param query: 查询参数
     * @return 结果
     */
    @Mapping(value = "/exist", method = MethodType.GET)
    public R<Boolean> exist(@Body TbyCategoryDictQuery query) {
        return this.tbyCategoryDictService.checkCode(query.getId(), query.getCode());
    }

}
